package com.heima.user.service.impl;

import apis.wemedia.IWemediaClient;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constants.UserContants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.LoginDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.service.ApUserAuthService;
import com.heima.user.service.ApUserService;
import com.heima.utils.common.AppJwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;


@Service
@Slf4j
@Transactional
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {


    @Autowired
    private IWemediaClient wemediaClient;

    /**
     * @param loginDto
     * @Description: App用户和游客登录
     * @Param:
     * @return: com.heima.model.common.dtos.ResponseResult
     * @Author: tyz
     * @Date: 2023/6/4
     */
    @Override
    public ResponseResult login(LoginDto loginDto) {
        //1，校验参数
        if (loginDto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //1.验证用户类型还是游客类型
        Map<String, Object> res = new HashMap<>();
        if (StringUtils.isNotEmpty(loginDto.getPhone()) && StringUtils.isNotEmpty(loginDto.getPhone())) {
            //2.判断为用户
            ApUser user = getOne(Wrappers.<ApUser>lambdaQuery().eq(ApUser::getPhone, loginDto.getPhone()));
            //判断是否又该手机号
            if (user == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户不存在");
            }
            //密码对比
            String password = DigestUtils.md5DigestAsHex((loginDto.getPassword() + user.getSalt()).getBytes());
            if (password.equals(user.getPassword())) {
                //返回token
                String token = AppJwtUtil.getToken(user.getId().longValue());
                Map<String, Object> userMap = new HashMap<>();
                userMap.put("name", user.getName());
                userMap.put("id", user.getId());
                userMap.put("phone", user.getPhone());
                res.put("user", userMap);
                res.put("token", token);
                return ResponseResult.okResult(res);
            }
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        } else if (StringUtils.isEmpty(loginDto.getPhone()) && StringUtils.isEmpty(loginDto.getPassword())) {
            String token = AppJwtUtil.getToken(0L);
            res.put("token", token);
            return ResponseResult.okResult(res);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "信息录入不完全");
    }

    /**
     * 用户审核后保持账户同步
     * @param userId
     */
    @Override
    @Async
    public void userAuthKeep(Integer userId) {
        ApUser apUser = getById(userId);
        apUser.setCertification(UserContants.USER_IS_CERTIFICATION);
        apUser.setIdentityAuthentication(UserContants.USER_IS_IDENTITY_AUTH);
        apUser.setFlag(UserContants.USER_TYPE_WEMEDIA);
        updateById(apUser);
        ResponseResult responseResult = wemediaClient.WeUserKeep(apUser);
    }
}
